<?php

class CItem extends Zend_Db_Table
{

    protected $_name;

    protected $_primary;

    protected $typeTable=null;

    public function _setup()
    {

        $this->_name=R_PRE."items";

        $this->_primary="itemid";

        parent::_setup();

    }

    public function setTypeTable($typeTable)
    {

        $this->typeTable=$typeTable;

    }

    public function createItem($data)
    {

        $itemid=$this->insert($data);

        return $itemid;

    }

    public function editItem($itemid,$data)
    {

        $db=$this->getAdapter();

        $where = $db->quoteInto('itemid = ?', $itemid);

        $rows_affected=$db->update($this->_name,$data,$where);

        if($rows_affected!=1)
        {

            return false;

        }else
        {

            return true;

        }

    }
    public function getitemid($itemid)
    {
        $db=$this->getAdapter();
        $select = $db->select();
        $select->from(R_PRE."products",'*');
        $select->where('productid = ? ',$itemid);
        $idArray = $db->fetchAll($select);
        // return $idArray[0]['itemid'];


        $db2=$this->getAdapter();
        $select2 = $db2->select();

        $select2->from(R_PRE."items",'*');
        $select2->where('itemid = ? ',$idArray[0]['itemid']);
        $idccArray = $db2->fetchAll($select2);
        return $idccArray[0]['flid'];


    }
    public function getitemfuid($itemid)
    {
        $retunstr;
        $db=$this->getAdapter();
        $select = $db->select();
        $select->from(R_PRE."productfls",'*');
        $select->where('flid = ? ',$itemid);
        $fuArray = $db->fetchAll($select);
        if($fuArray[0]['pid']!=0)
        {
            $retunstr = $this->getitemfuid($fuArray[0]['pid']);
        }else
        {
            $retunstr = $fuArray[0]['flid'];
        }
        return $retunstr;
    }
    
    public function getDetail($itemid,$type,$hasfl=true)
    {

        $db=$this->getAdapter();

        $select = $db->select();

        $itemTable=R_PRE."items";

        if($this->typeTable==null)
        {

            $fenleiTable=R_PRE.$type."fls";

        }else
        {

            $fenleiTable=$this->typeTable;

        }

        $typeTable=R_PRE.$type."s";

        $select->from($itemTable,'*');

        $select->join($typeTable,"$itemTable.itemid=$typeTable.itemid",'*');

        if($hasfl)
        {

            $select->joinLeft($fenleiTable,"$itemTable.flid=$fenleiTable.flid",'*');

        }

        $select->where($itemTable.'.itemid = ? ',$itemid);

        return $db->fetchAll($select);

    }

    public function delItems($itemidArray)
    {

        $db=$this->getAdapter();

        $itemidString=join(',',$itemidArray);

        $result=$db->query("DELETE FROM $this->_name WHERE itemid IN ($itemidString)");

        return $result;

    }



    public function getAllItemCounts($type,$flid=0,$uid=0,$is_banned="has_banned",$where='')
    {

        $db=$this->getAdapter();

        $select = $db->select();

        $select->from($this->_name);

        if($where!='')
        {

            $typeTable=R_PRE.$type."s";

            $select->join($typeTable, $this->_name.".itemid = $typeTable.itemid");

            $select->where($where);

        }

        if($flid!=0&&$flid!='')
        {

            $select->where(' flid = ? ',$flid);

        }

        if($uid!=0&&$uid!='')
        {

            $select->where(' uid = ? ',$uid);

        }

        if($is_banned=="no_banned")
        {

            $select->where('is_banned = ? ',0);

        }

        $select->where(' item_type = ? ',$type);

        return count($db->fetchAll($select));

    }

    public function getAllItems($from,$counts,$type,$flid=0,$uid=0,$is_banned="has_banned",$where='',$order='add_time DESC')
    {

        $db=$this->getAdapter();

        $itemTable=$this->_name;

        $typeTable=R_PRE.$type."s";

        if($this->typeTable==null)
        {

            $typeFlTable=R_PRE.$type."fls";

        }else
        {

            $typeFlTable=$this->typeTable;

        }

        $select = $db->select();

        $select->from($itemTable,'*');

        $select->join($typeTable, "$itemTable.itemid = $typeTable.itemid", '*');

        if($flid!=0&&$flid!='')
        {

            $select->join($typeFlTable, "$itemTable.flid = $typeFlTable.flid", '*');

            $select->where($itemTable.'.flid = ? ',$flid);

        }else
        {

            $select->joinLeft($typeFlTable, "$itemTable.flid = $typeFlTable.flid", '*');

        }

        if($uid!=0&&$uid!='')
        {

            $select->where($itemTable.'.uid = ? ',$uid);

        }else
        {

            $select->join(R_PRE.'members', R_PRE.'members.uid = '.$itemTable.'.uid', '*');

        }

        //仅限产品
        if($type=='product')
        {

        //$featureTable=R_PRE.'features';

        //$select->joinLeft($featureTable,$featureTable.'.featureid='.$typeTable.'.featureid','name AS feature_name');

        }

        if($is_banned=="no_banned")
        {

            $select->where($itemTable.'.is_banned = ? ',0);

        }

        if($where!='')
        {

            $select->where($where);

        }

        $select->order("$itemTable.$order");

        if($counts!=0)
        {

            $select->limit($counts, $from);

        }

        return $db->fetchAll($select);

    }

    public function addViewNum($itemid)
    {

        $db=$this->getAdapter();

        return $db->query("UPDATE $this->_name SET view_num=view_num+1 WHERE itemid='$itemid'");

    }

    public function getViewNum($itemid)
    {

        $db=$this->getAdapter();

        $select=$db->select();

        $select->from($this->_name,'view_num');

        $select->where('itemid=?',$itemid);

        return $db->fetchOne($select);

    }

    public function addReplyNum($itemid)
    {

        $db=$this->getAdapter();

        return $db->query("UPDATE $this->_name SET reply_num=reply_num+1 WHERE itemid='$itemid'");

    }

    public function getItemsByItemid($itemid,$pattern_name,$amounts)
    {

        $db=$this->getAdapter();

        $priceTable=R_PRE.'product_price';

        $productTable=R_PRE.'products';

        $memberTable=R_PRE.'members';

        $locationTable=R_PRE.'locationfls';

        return $db->fetchAll("SELECT * FROM $this->_name,
						(
							SELECT net_price,pattern_name,min_counts,max_counts FROM $priceTable 
							WHERE pattern_name='$pattern_name' 
							AND min_counts<='$amounts' 
							AND max_counts>='$amounts' 
							AND itemid='$itemid'
						) AS A,$productTable,$memberTable,$locationTable  
						WHERE $this->_name.itemid='$itemid' 
						AND $this->_name.itemid=$productTable.itemid 
						AND $memberTable.uid=$this->_name.uid 

        ");

    }





}